/*******************************************************************************
 *  Imixs IX Workflow Technology
 *  Copyright (C) 2003, 2008 Imixs Software Solutions GmbH,  
 *  http://www.imixs.com
 *  
 *  This program is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 *  General Public License for more details.
 *  
 *  You can receive a copy of the GNU General Public
 *  License at http://www.gnu.org/licenses/gpl.html
 *  
 *  Contributors:  
 *  	Imixs Software Solutions GmbH - initial API and implementation
 *  	Ralph Soika
 *  
 *******************************************************************************/
package org.imixs.manik.webstat.web;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.imixs.manik.webstat.ejb.AccessLogService;
import org.imixs.manik.webstat.jpa.AccessLog;
import org.imixs.manik.webstat.jpa.LogFile;

/**
 * This backing bean is an example how to interact with the EntityService to
 * manage ItemCollections as instances of Entities
 * <p>
 * The bean provides an ItemCollectionAdapter to simplify to access properties
 * of the ItemCollection from an JSP page.
 * <p>
 * 
 * @see team.xhtml, teamlist.xhtml
 * 
 * @author rsoika
 * 
 */

@ManagedBean(name = "accessLogMB")
@SessionScoped
public class AccessLogMB {

	private AccessLog currentLog;

	private List<AccessLog> logs = null;

	private static Logger logger = Logger.getLogger("org.imixs.manik.webstat");

	@EJB
	AccessLogService accessLogService;

	public AccessLogMB() {
		super();

	}

	/**
	 * The init method is used to add necessary indices to the entity index list
	 * if index still exists the method did change any data
	 */
	@PostConstruct
	public void init() {
		//

	}

	public AccessLog getAccessLog() {
		if (currentLog == null)
			this.doCreate(null);
		return currentLog;
	}

	public void setAccessLog(AccessLog currentDomain) {
		this.currentLog = currentDomain;
	}

	/**
	 * This method is called by a jsf page to create a new instance of a team
	 * entity.
	 * 
	 * This method creates an empty ItemCollection
	 * 
	 * @param event
	 * @return
	 */
	public void doCreate(ActionEvent event) {
		currentLog = new AccessLog();
	}

	/**
	 * saves the current analyzer.
	 * 
 * 
	 * @param event
	 * @return
	 * @throws Exception
	 */
	public void doSave(ActionEvent event) throws Exception {
		
		
		// save Log now...
		accessLogService.save(currentLog);
		doReset(event);
	}

	/**
	 * this method is called by datatables to select an workitem
	 * 
	 * @return
	 */

	public String editAccessLog() {
		return "/pages/domain";
	}

	/**
	 * this method removes the current selected worktiem from a view
	 * 
	 * @param event
	 * @throws Exception
	 */
	public String deleteAccessLog() {
		logger.info(" starte remove...");
		try {
			accessLogService.remove(currentLog);
		} catch (Exception e) {
			logger.severe(" Error deleting AccessLog - " + e.getMessage());
			e.printStackTrace();
		}

		doReset(null);

		return "/pages/accessloglist";
	}

	/**
	 * resets the current loglist
	 * 
	 * @return
	 */
	public void doReset(ActionEvent event) {
		logs = null;
	}

	public List<AccessLog> getAccessLogs() {
		if (logs == null)
			logs = accessLogService.getAccessLogs();
		return logs;
	}

	/**
	 * Returns a list of Log files scanned by the AccessLog Service
	 * 
	 * @return
	 */
	public List<LogFile> getLogFiles() {

		return accessLogService.getLogFiles(currentLog);

	}

	/**
	 * scans the log files for new entries
	 * 
	 * @return
	 */
	public void doScan(ActionEvent event) {
		try {

			accessLogService.scan(currentLog);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * removes all entities from this AccessLog
	 * 
	 * @return
	 */
	public void doClear(ActionEvent event) {
		try {
			accessLogService.clear(currentLog);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	
	
}
